import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
import com.pi4j.util.Console;


public class bujindianji {

	public static void main(String[] args) throws InterruptedException {
		System.out.println("<--Pi4J--> GPIO Control Example ... started.");
		Console console = new Console();
        // create gpio controller
		final GpioController gpio = GpioFactory.getInstance();
		// provision gpio pin #01 as an output pin and turn on
        final GpioPinDigitalOutput pin1 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "r1", PinState.HIGH);
        final GpioPinDigitalOutput pin4 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "r2", PinState.HIGH);
        final GpioPinDigitalOutput pin5 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "r3", PinState.HIGH);
        final GpioPinDigitalOutput pin6 = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "r4", PinState.HIGH);
        // set shutdown state for this pin
        pin1.setShutdownOptions(true, PinState.LOW);
        pin4.setShutdownOptions(true, PinState.LOW);
        pin5.setShutdownOptions(true, PinState.LOW);
        pin6.setShutdownOptions(true, PinState.LOW);
        
        Thread.sleep(1000);
        String arg1= args[1];
        int count = 1;
        int j = 0;
        String director = args[0];
        if (arg1 != null) {
			count = Integer.parseInt(arg1);
		}
        while (!console.exiting()) {
        	for (int i = 0; i < 64; i++) {;
        		if (director != null && director.equals("few")) {
        			few(gpio,pin1,pin4,pin5,pin6);
				}else {
					rev(gpio,pin1,pin4,pin5,pin6,5);
				}
            	
    		}
        	System.out.println("count:"+count);
//        	Thread.sleep(3000);
        	j++;
		}
        gpio.shutdown();
//        System.out.println("--> GPIO shutdown!!");

	}
	
	public static void few(GpioController gpio,GpioPinDigitalOutput pin1, GpioPinDigitalOutput pin4,
			GpioPinDigitalOutput pin5,GpioPinDigitalOutput pin6) throws InterruptedException{
		
		//1
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
		
      //2
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
        
      //3
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        //4
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        //5
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        //6
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        //7
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        //8
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
	}
	
	public static void rev(GpioController gpio,GpioPinDigitalOutput pin1, GpioPinDigitalOutput pin4,
			GpioPinDigitalOutput pin5,GpioPinDigitalOutput pin6,int i) throws InterruptedException{

        //8
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
//        //7
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
      //6
        pin1.setState(PinState.HIGH);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
      //5
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
      //4
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.HIGH);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
      //3
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.LOW);
        Thread.sleep(5);
        
      //2
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.HIGH);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
        //1
        pin1.setState(PinState.LOW);
        pin4.setState(PinState.LOW);
        pin5.setState(PinState.LOW);
        pin6.setState(PinState.HIGH);
        Thread.sleep(5);
	
		
//        pin1.setState(PinState.LOW);
//        pin4.setState(PinState.HIGH);
//        pin5.setState(PinState.LOW);
//        pin6.setState(PinState.LOW);
//        Thread.sleep(4);
        
//		pin1.setState(PinState.HIGH);
//        pin4.setState(PinState.LOW);
//        pin5.setState(PinState.LOW);
//        pin6.setState(PinState.LOW);
//        Thread.sleep(10);
//        pin1.setState(PinState.LOW);
//        pin4.setState(PinState.HIGH);
//        pin5.setState(PinState.LOW);
//        pin6.setState(PinState.LOW);
//        Thread.sleep(8);
//        
//        pin1.setState(PinState.LOW);
//        pin4.setState(PinState.LOW);
//        pin5.setState(PinState.HIGH);
//        pin6.setState(PinState.LOW);
//        Thread.sleep(8);
//        
//        pin1.setState(PinState.LOW);
//        pin4.setState(PinState.LOW);
//        pin5.setState(PinState.LOW);
//        pin6.setState(PinState.HIGH);
//        Thread.sleep(8);
        
	}
}
